﻿#include"snake.h"
void SetPos(short x,short y)
{
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x , y };
	SetConsoleCursorPosition(houtput, pos);
}
void SnakeInit(pSnake ps)
{
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("SnakeInit()::malloc");
			return;
		}
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;
		cur->next = NULL;
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_dir = RIGHT;
	ps->_FoodWeight = 10;
	ps->_score = 0;
	ps->_status = OK;
	ps->_SleepTime = 200;

}
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
	again:
	do
	{
		x = rand() % 55 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood()::malloc");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);
	ps->_pFood = pFood;
}
void CreateMap()
{
	for (int i = 0; i < 30; i++)
	{
		wprintf(L"%lc", WALL);
	}
	SetPos(0, 26);
	for (int i = 0; i < 30; i++)
	{
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(58, i);
		wprintf(L"%lc", WALL);
	}
}
void WelcomeToGame()
{
	SetPos(40,13);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(42,19);
	system("pause");
	system("cls");
	SetPos(36,13);
	wprintf(L"按 ↑ ↓ ← → 来控制蛇的移动\n");
	SetPos(36,14);
	wprintf(L"按F3来加速，F4来减速\n");
	SetPos(36,15);
	wprintf(L"加速可以得到更高的分数\n");
	SetPos(42, 19);
	system("pause");
	system("cls");
}
void GameStart(pSnake ps)
{
	system("mode con cols=100 lines=30");
	system("title");
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(houtput, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(houtput, &CursorInfo);
	WelcomeToGame();
	CreateMap();
	SnakeInit(ps);
	CreateFood(ps);
}
#define KEY_PRESS(vk) (GetAsyncKeyState(vk)?1:0)
void pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
void noFood(pSnake ps, pSnakeNode pn)
{
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next != NULL)
	{
		SetPos(cur->x,cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}
void EatFood(pSnake ps, pSnakeNode pn)
{
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;
	free(pn);
	pn = NULL;
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_score += ps->_FoodWeight;
	CreateFood(ps);
}
int NextIsFood(pSnake ps, pSnakeNode pn)
{
	if (pn->x == ps->_pFood->x && pn->y == ps->_pFood->y)
		return 1;
	else
		return 0;
}
void SnakeMove(pSnake ps)
{
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->_dir)
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}
	if (NextIsFood(ps,pNextNode))
	{
		EatFood(ps,pNextNode);
	}
	else
	{
		noFood(ps, pNextNode);
	}
}
void PrintHelpInfo()
{
	SetPos(64,15);
	wprintf(L"按 ↑ ↓ ← → 来控制蛇的移动\n");
	SetPos(64,16);
	wprintf(L"按F3来加速，F4来减速\n");
	SetPos(64,17);
	wprintf(L"加速可以得到更高的分数\n");
	SetPos(64,18);
	wprintf(L"不可以撞墙和咬自己\n");
	SetPos(64,22);
	printf("@EA_GAME\n");

}
void GameRun(pSnake ps)
{
	PrintHelpInfo();
	do
	{
		SetPos(64, 12);
		printf("当前食物分数：%2d", ps->_FoodWeight);
		SetPos(64, 13);
		printf("总分数：%d", ps->_score);
		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_SleepTime > 80)
			{
				ps->_FoodWeight += 2;
				ps->_SleepTime -= 30;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_FoodWeight > 2)
			{
				ps->_FoodWeight -= 2;
				ps->_SleepTime += 30;
			}
		}
		SnakeMove(ps);
		Sleep(ps->_SleepTime);
	} while (ps->_status == OK);
}